package presenters;

import java.sql.ResultSet;
import java.sql.SQLException;

import model.ConnectionManager;
import model.DbSchema;
import model.IPresenter;
import model.Model;
import views.PersonalInfoPanel;

public class PersonalInfoPresenter extends IPresenter {
	private PersonalInfoPanel myPanel;

	public PersonalInfoPresenter(PersonalInfoPanel p) {
		super();
		myPanel = p;
		
		if(Model.getCurrentUser() != null) {
			updateFields();
		}
	}
	
	public String[] updateFields() {
		String sql = "SELECT * FROM " + ConnectionManager.GROUP 
				+ ".MEMBER WHERE USERNAME = '" + Model.getCurrentUser() + "';";
		String[] out = new String[2];
		ResultSet rs = executeSQLQuery(sql);
		try {
			rs.first();
			String fName = rs.getString("FIRST_NAME");
			String lName = rs.getString("LAST_NAME");
			String mInit = rs.getString("MIDDLE_INIT");
			String address = rs.getString("ADDRESS");
			String phone = rs.getString("PHONE");
			String email = rs.getString("EMAIL");
			String plan = rs.getString("PLAN_NAME");
			String card = rs.getString("CARD_NO");
			
			myPanel.setFields(fName, mInit, lName, email, phone, address);
			out[0] = plan;
			out[1] = card;
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		return out;
	}

	public void addInfo(String fName, String mInitial, String lName,
			String email, String phone, String address, String plan_name, String card_no) {

		if(checkString(fName) && checkString(mInitial) && checkString(lName) && 
				checkString(email) && checkString(phone)) {
			if((address != null) && !address.isEmpty()) {
				//INSERT INTO MEMBER VALUES(username, first_name, 
				//		last_name, middle_init, address, phone, email, plan_name, card_no);
				String username = Model.getCurrentUser();
				
				String sqlIf = "IF (SELECT COUNT(*) "
						+ "FROM " + ConnectionManager.GROUP + ".MEMBER "
						+ "WHERE USERNAME = '" + username + "' "
						+ "GROUP BY USERNAME "
						+ " IS NULL) THEN ";
				
				String sqlInsert = "INSERT INTO "+ ConnectionManager.GROUP + ".MEMBER("
				+ DbSchema.MT_USERNAME + ", "
				+ DbSchema.MT_FNAME + ", "
				+ DbSchema.MT_LNAME + ", "
				+ DbSchema.MT_MINIT + ", "
				+ DbSchema.MT_ADDR + ", "
				+ DbSchema.MT_PHONE + ", "
				+ DbSchema.MT_EMAIL + ", "
				+ DbSchema.MT_PLAN_NAME + ", "
				+ DbSchema.MT_CARD_NO + ")"
				+ " VALUES('" 
						+ username + "', '"
						+ fName + "', '"
						+ lName + "', '"
						+ mInitial + "', '"
						+ address + "', '"
						+ phone + "', '"
						+ email + "', '"
						+ plan_name + "', '"
						+ card_no + "');";
				
				String sqlElse = " ELSE ";
				
				String sqlUpdate = 
						"UPDATE Member "
							+ "SET "
								+ "FIRST_NAME = '" 		+ fName 	+ "', "
								+ "LAST_NAME = '" 		+ lName 	+ "', "
								+ "MIDDLE_INIT = " 		+ mInitial 	+ "', "
								+ "ADDRESS = '" 		+ address 	+ "', "
								+ "PHONE = '" 			+ phone 	+ "', "
								+ "EMAIL = '" 			+ email 	+ "', "
								+ "PLAN_NAME = '" 		+ plan_name + "', "
								+ "CARD_NO = '" 		+ card_no 	+ "' "
							+ "WHERE (Username = " 		+ username 	+ ");";
				
				String sql = sqlIf + sqlInsert + sqlElse + sqlUpdate;
				int out = executeSQLUpdate(sql);
				System.out.println("Information added at row: " + out);
			} // close if
		} // close checkString if
	} // close addInfo
		

}
